package com.payment.ktb.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.payment.ktb.application.MyApplication;


/**
 * Created by syd on 2016/6/2.
 */
public class DeviceUtils {


    /**
     * 获取设备的DeviceId
     * @return
     */
    public static String getDeviceId(){
        TelephonyManager mTelephonyMgr = (TelephonyManager) MyApplication.getContext().getSystemService(Context.TELEPHONY_SERVICE);
        String imei = mTelephonyMgr.getDeviceId();
        return imei==null ? "555555" : imei;
    }

    /**
     * 获取设备的版本号
     */
    public static String getVersionName() {
        PackageManager manager = MyApplication.getContext().getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(MyApplication.getContext().getPackageName(),0);
            return info.versionName == null ? "0.0.0" : info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            ToastUtils.showToast("获取设备版本号失败！");
        }
      return "0.0.0";
    }

    /**
     * 获取设备的VersionCode
     */
    public static int getVersionCode() {
        PackageManager manager = MyApplication.getContext().getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(MyApplication.getContext().getPackageName(),0);
            return info.versionCode ;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            ToastUtils.showToast("获取设备版本失败！");
        }
      return 0;
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(float dpValue) {
        final float scale = MyApplication.getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    /**
     * 获取DisplayMetrics
     * @return
     */
    public static DisplayMetrics getDisplayMetrics () {

        WindowManager manager = (WindowManager) MyApplication.getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics;
    }



}
